//统一封装接口方法
//每个方法负责一个请求一个url地址
// 逻辑页面，导入这个接口方法，就能发送请求
// 好处:请求url地址，可以在这里统一管理

import request from "@/utils/request.js";
import axios from "@/utils/request.js";
import { getToken } from "@/utils/token";
import type from "postcss-pxtorem/lib/type";

//用户请求登录的接口
//使用axios 会自动将数据转换成 json 格式，并且会带上请求头
export const loginAPI = ({mobile,code}) => request({
    url:'/v1_0/authorizations',
    method:'POST',
    data:{
        mobile,
        code
    }

})

// 获取全部频道
export const getAllChannelsAPI = () => request({
        url:'/v1_0/channels',
        method:'GET'
    })

//获取用户频道

export const getUserChannelsAPI = () => request({
    url: '/v1_0/user/channels',
    
})

//获取文章列表

export const getAllArticleListAPI = ( {channel_id,timestamp} ) => request({
    url:'/v1_0/articles',
    
    params:{
        // channel_id:channelId,
        // timestamp:(new Date()).getTime()
        channel_id,
        timestamp
    }
})

// 对文章不感兴趣 
export const dislikeArticleAPI = ({artId}) => request({
    url:'/v1_0/article/dislikes',
    method:'POST',
    
    //接收不喜欢文章的id
    data: {
        target:artId
    }
})

//举报文章 ,反馈垃圾内容

export const reportArticleAPI = ({artId,type}) => request({
    url:'/v1_0/article/reports',
    method:'POST',
    data: {
        target: artId,
        type: type,
        remake: "随便写点"

    }

})

//设置用户的频道 ，部分覆盖

export const updateChannelAPI = ({ channels }) => request({
    url:'/v1_0/user/channels',
    method:'PATCH',
    data:{
        channels
    }

})

//搜索建议栏
export const suggestionAPI = ({ keywords }) => request ({
    url:'/v1_0/suggestion',
    params:{
       q:keywords
    }

})

// 获取搜索结果 
export const searchResultAPI = ({ page=1 , per_page=10 , q}) => request ({
    url:'/v1_0/search',
    params:{
        page,
        per_page,
        q
    }
})
//刷新token 信息
export const reFreshTokenAPI =() =>request({
    url:'/v1_0/authorizations',
    method:'PUT',
    headers:{
        Authorization:'Bearer ' + localStorage.getItem('refresh_token')
    }
})


//获取个人信息
export const UserAPI = () =>request({
    url:'/v1_0/user/profile',
    method:'GET'
})

//获取用户自己信息 
export const userInfoAPI =() =>request({
    url:'/v1_0/user'
})

//上传自己的头像 (换头像)
//参数类型为 formdata 不能用 对象传参，使用对象传参 ，axios 会自动带上 application/json的请求头，
// 不写的话，浏览器会根据传递的文件类型，加上请求头

// export const changePhotoAPI = (fd) =>request({
//     url:'/v_1/user/photo',
//     method:'PATCH',
//     data:fd
// })

//编辑用户头像
export const changePhotoAPI =(fd) =>{ 
    return request({
        url:'/v1_0/user/photo',
        method:'PATCH',
        data:fd,
        // headers:{
        //     'Content-Type ':'multipart/form-data'
        // }
    })
}


// 编辑用户信息
export const editUserInfo = (obj) => request({
    url:'/v1_0/user/profile',
    method:'PATCH',
    // data:{
    //     name:name,
    //     birthday:birthday
    // }
    // 1.传递什么参数由前端决定
    // 2.判断传递的参数是否为空，不为空，就将属性加入进去
    data:obj
})

//查看关注人的信息
export const checkFollow = ({pages,per_pages}) => request({
    url:'/v1_0/user/followings',
    method:'get',
    params:{
        page:pages,
        per_page:per_pages
    }
})

//获取评论
export const commentAPI = ({type,article_id,offset = null,limit = 5}) =>request({
    url:'/v1_0/comments',
    params:{
        type:type,
        source:article_id,
        offset,
        limit,
    }
})

//发表评论

// 当参数使用请求体方式的时候，参数为 空了，也会继续给后端发送 参数和值，
//只有 params 请求，值为null时会忽略
export const submitAPI = ({article_id, content , art_id = null}) => {
    const obj = {
        target:article_id,
        content
    }
    if(art_id !== null){
        obj.art_id = art_id
    }
    return request({
    url:'/v1_0/comments',
    method:'post',
    data:obj
        // target:article_id,
        // content:content,
        // art_id
        
    
})
}
    

//点赞评论,或者对评论的回复点赞
export const commentLikingAPI = ({commentId}) =>request({
    url:'/v1_0/comment/likings',
    method:'POST',
    data:{
        target:commentId
    }
})
//取消对评论的点赞，或者取消对评论回复点赞
export const commentUnLikingAPI =({commentId}) =>request({
    url:`/v1_0/comment/likings/${commentId}`,
    method:'DELETE'
})


// 点赞文章
export const loveArticleAPI = ({article_id}) =>request({
    url:'/v1_0/article/likings',
    method:'POST',
    data:{
        target:article_id
    }
})
//取消文章点赞
export const unLoveArticleAPI =({article_id}) => request({
    url:`/v1_0/article/likings/${article_id}`,
    method:'DELETE'
})

//关注作者

export const userFollowAPI = ({userId}) => request ({
    url:'/v1_0/user/followings',
    method:'POST',
    data:{
        target:userId
    }
})

//取关作者  
export const userUnFollowAPI =({userId}) =>request({
    url:`/v1_0/user/followings/${userId}`,
    method:'DELETE',
})


//获取搜索详情 路径传参
export const searchDetailAPI = ({ article_id }) => request({
    url:`/v1_0/articles/${article_id}`
})


// 删除频道

export const deleteChannelAPI = ({channelId}) => request({
    url:`/v1_0/user/channels/${channelId}`,
    method:'DELETE'
})





